home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk192 / unzipv3.1 / match.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  4KB  |  187 lines

  1. /*
  2.  *  arcmatch.c  1.1
  3.  *
  4.  *  Author: Thom Henderson
  5.  *  Original System V port: Mike Stump
  6.  *
  7.  * REVISION HISTORY
  8.  *
  9.  * 03/22/87  C. Seaman      enhancements, bug fixes, cleanup
  10.  * 11/13/89  C. Mascott     adapt for use with unzip
  11.  * 01/25/90  J. Cowan       match case-insensitive
  12.  * 03/17/90  D. Kirschbaum      Prototypes, other tweaks for Turbo C.
  13.  *
  14.  */
  15.  
  16. /*
  17.  * ARC - Archive utility - ARCMATCH
  18.  * 
  19.  * Version 2.17, created on 12/17/85) at 20:32:18
  20.  *
  21.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  22.  *
  23.  *     Description:
  24.  *        This file contains service routines needed to maintain an archive.
  25.  */
  26. #ifdef    AMIGA
  27. #include <stdio.h>      /* for printf() */
  28. #include <stdlib.h>     /* for exit() */
  29. #include "unzip.h"
  30. #include "zip_proto.i"
  31. #else
  32. #include <sys/types.h>
  33. #include <sys/dir.h>
  34. #endif
  35. #include <ctype.h>
  36.  
  37. #ifdef __TURBOC__               /* v2.0b */
  38. #include <stdio.h>      /* for printf() */
  39. #include <stdlib.h>     /* for exit() */
  40. #endif
  41.  
  42. #ifdef __TURBOC__              /* v2.0b */
  43. /* local prototypes for Turbo */
  44.  
  45. int match(char *string, char *pattern);
  46. BOOLEAN do_list (register char *string, char *pattern);  /* v2.0b */
  47. void list_parse (char **patp, char *lowp, char *highp);
  48. char nextch (char **patp);
  49. #else       /* v2.0b original code */
  50. BOOLEAN do_list();
  51. char nextch();
  52. void list_parse();
  53. #endif
  54.  
  55. int match(string, pattern)
  56. char *string;
  57. char *pattern;
  58. {
  59.     register int ismatch;
  60.  
  61.     ismatch = FALSE;
  62.     switch (*pattern)
  63.     {
  64.     case ASTERISK:
  65.         pattern++;
  66.         do
  67.         {
  68.             ismatch = match (string, pattern);
  69.         }
  70.         while (!ismatch && *string++ != EOS);
  71.         break;
  72.     case QUESTION:
  73.         if (*string != EOS)
  74.             ismatch = match (++string, ++pattern);
  75.         break;
  76.     case EOS:
  77.         if (*string == EOS)
  78.             ismatch = TRUE;
  79.         break;
  80.     case LEFT_BRACKET:
  81.         if (*string != EOS)
  82.             ismatch = do_list (string, pattern);
  83.         break;
  84.     case BACK_SLASH:
  85.         pattern++;
  86.     default:
  87.     if (toupper(*string) == toupper(*pattern))
  88.         {
  89.             string++;
  90.             pattern++;
  91.             ismatch = match (string, pattern);
  92.         }
  93.         else
  94.             ismatch = FALSE;
  95.         break;
  96.     }
  97.     return(ismatch);
  98. }
  99.  
  100. BOOLEAN do_list (string, pattern)
  101. register char *string;
  102. char *pattern;
  103. {
  104.     register BOOLEAN ismatch;
  105.     register BOOLEAN if_found;
  106.     register BOOLEAN if_not_found;
  107.     auto char lower;
  108.     auto char upper;
  109.  
  110.     pattern++;
  111.     if (*pattern == '!')
  112.     {
  113.         if_found = FALSE;
  114.         if_not_found = TRUE;
  115.         pattern++;
  116.     }
  117.     else
  118.     {
  119.         if_found = TRUE;
  120.         if_not_found = FALSE;
  121.     }
  122.     ismatch = if_not_found;
  123.     while (*pattern != ']' && *pattern != EOS)
  124.     {
  125.         list_parse(&pattern, &lower, &upper);
  126.         if (*string >= lower && *string <= upper)
  127.         {
  128.             ismatch = if_found;
  129.             while (*pattern != ']' && *pattern != EOS) pattern++;
  130.         }
  131.     }
  132.  
  133.     if (*pattern++ != ']')
  134.     {
  135.         printf("Character class error\n");
  136.         exit(1);
  137.     }
  138.     else
  139.         if (ismatch)
  140.             ismatch = match (++string, pattern);
  141.  
  142.     return(ismatch);
  143. }
  144.  
  145. void list_parse (patp, lowp, highp)
  146. char **patp;
  147. char *lowp;
  148. char *highp;
  149. {
  150.     *lowp = nextch (patp);
  151.     if (**patp == '-')
  152.     {
  153.         (*patp)++;
  154.         *highp = nextch(patp);
  155.     }
  156.     else
  157.         *highp = *lowp;
  158. }
  159.  
  160. char nextch (patp)
  161. char **patp;
  162. {
  163.     register char ch;
  164.     register char chsum;
  165.     register INT count;
  166.  
  167.     ch = *(*patp)++;
  168.     if (ch == '\\')
  169.     {
  170.         ch = *(*patp)++;
  171.         if (IS_OCTAL (ch))
  172.         {
  173.             chsum = 0;
  174.             for (count = 0; count < 3 && IS_OCTAL (ch); count++)
  175.             {
  176.                 chsum *= 8;
  177.                 chsum += ch - '0';
  178.                 ch = *(*patp)++;
  179.             }
  180.             (*patp)--;
  181.             ch = chsum;
  182.         }
  183.     }
  184.     return(ch);
  185. }
  186.  
  187.